<?php
class IndexAction extends Action {
	public function index() {
		$this->display ();
	}
	public function login() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$Dao = M ( "User" );
		if ($Dao->create ()) {
			$condition ['username'] = $Dao->username;
			$condition ['password'] = MD5 ( $Dao->password );
			// 查询数据
			$list = $Dao->where ( $condition )->find ();
			if ($list) {
				//echo '账号正确';
				session ( 'uid', $list ['uid'] );
				session ( 'username', $list ['username'] );
				session ( 'nickname', $list ['nickname'] );
				$this->redirect ( 'home' );
			} else {
				$this->error ( "账号/密码错误" );
			
			}
		}
	}
	public function logout() {
		
		session ( null );
		$this->display ( "index" );
	}
	
	public function home() {	
		
		
		if (session ( 'uid' )) {
			////
		import ( "@.ORG.Page" );
		$getname = session ( 'nickname' );
		
		
		$Dao = M ( "Mail" );
		
		// 查询数据
		$condition1 ['getname'] = $getname;
		$condition1 ['state'] = 0; //只显示未读信件
		
		$count1 = $Dao->where ( $condition1 )->count ();
		
		$p1 = new Page ( $count1, 10 );
		
		foreach ( $condition1 as $key => $val ) {
			$p1->parameter .= "$key=" . urlencode ( $val ) . "&";
		}
		// 分页显示输出
		$p1->setConfig('header', '条未读信息');
		$page1 = $p1->show ();
		
		// 当前页数据查询
		

		$maillist = $Dao->where ( $condition1 )->order ( 'senddate ASC' )->limit ( $p1->firstRow . ',' . $p1->listRows )->select ();
		
		$this->assign ( 'page1', $page1 );
		$this->assign ( 'maillist', $maillist );
		
			
		$Pname = $_GET ["pname"];
		$Managername = $_GET ["managername"];
		$Fund = $_GET ["fund"];
		
		$Dao = D( "ProjectView" );
	
			
		$condition2 ['pname'] = array ('like', '%' . $Pname . '%' );
		$condition2 ['managername'] = array ('like', '%' . $Managername . '%' );
		$condition2 ['fund'] = array ('like', '%' . $Fund . '%' );
		
		$count2 = $Dao->where ( $condition2 )->count ();
		
		
		$p2 = new Page ( $count2, 10 );
		foreach ( $condition2 as $key => $val ) {
			$p2->parameter .= "$key=" . urlencode ( $val ) . "&";
		}
		// 分页显示输出
		$p2->setConfig('header', '个项目');
		$page2 = $p2->show ();
		
		$projlist = $Dao->where ( $condition2 )->order ( 'pname ASC' )->limit ( $p2->firstRow . ',' . $p2->listRows )->select ();
		
		$this->assign ( 'page2', $page2 );
		$this->assign ( 'projlist', $projlist );
		
		
		$this->display ();
		
		} else
			$this->error ( "尚未登录" );
	
	}
public function modify() {
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$Dao = D ( "User" );
		$condition ['uid'] =session ( 'uid' );
		$vo=$Dao->where($condition)->find();
		
		$this->assign('userinfo',$vo);
		$this->display();
	
	}
	
	public function updateUser() {
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$Dao = D ( "User" );
		
		if ($vo = $Dao->create ()) {
			$vo ['uid'] = session ( 'uid' );
			$result = $Dao->save ( $vo );
			if ($result !== false) {
				session ( null );
				
				$this->redirect ( 'index' );
			
			} else {
				
				$this->error ( $Form->getError () );
			}
		} else {
			$this->error ( $Form->getError () );
		}
	
	}
	public function regedit() {
		
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$Dao = D ( "User" );
		
		if ($Dao->create ()) {
			if ($lastInsId = $Dao->add ()) {
				$this->redirect ( 'index' );
			} else {
				$this->error ( "数据库插入错误" );
			}
		} else {
			$this->error ( $Dao->getError () );
		
		//exit($Dao->getError().' [ <a href="javascript:history.back()">返 回</a> ]');
		}
	
	}
	public function checkName() {
		$Dao = M ( "User" );
		$condition ['username'] = $_POST ['username'];
		// 查询数据
		$list = $Dao->where ( $condition )->select ();
		if ($list) {
			$this->success ( '用户名已存在，请更改！' );
		} else {
			$this->error ( '用户名可以注册' );
		}
	}
	
	public function test() {
		$Name = session ( 'nickname' );
		
		$Dao = M ( "Mail" );
		
		// 查询数据
		$condition ['getname'] = $Name;
		
		$count = $Dao->where ( $condition )->count ();
		echo $count;
	
	}
	public function test2() {
		
		$file = D ( "file" );
		//在这里应该从session中读取当前的project名称，然后从数据库中读取
		//$project=$_SESSION['project'];	
		//为了测试方便，这里只用project2
		$project = "project2";
		$condition ['project'] = $project;
		$list = $file->where ( $condition )->select ();
		$this->assign ( 'list', $list );
		$this->display ();
	
	}
	
	public function about() {
		$this->display ();
	}
	public function menutest1() {
		
		$this->display ();
	
	}
	public function tpl1()
	{
	session('project','殷三三');	
	$file=D("file");
	//在这里应该从session中读取当前的project名称，然后从数据库中读取
	//$project=$_SESSION['project'];	
	//为了测试方便，这里只用project2
	$project=$_SESSION['project'];
	$condition['project'] = $project;
	$list=$file->where($condition)->select();
	$this->assign('list',$list);
	$this->assign('project',$project);
	$this->assign('project',$_SESSION['project']);
		$this->display();
	}
}